package br.pucrs.lognplay.faces.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

import br.pucrs.lognplay.entity.User.Role;

@ManagedBean
@SessionScoped
public class MenuBean extends Bean {

	private MenuModel menuModel;

	public MenuBean() {
		menuModel = new DefaultMenuModel();

		// Home
		MenuItem homeMenuItem = new MenuItem();
		homeMenuItem.setValue("Home");
		homeMenuItem.setUrl("/protected/client/home.xhtml");
		homeMenuItem.setIcon("ui-icon-home");
		menuModel.addMenuItem(homeMenuItem);

		Role loggedUserRole = getLoggedUser().getRole();

		// Client
		if (loggedUserRole.equals(Role.MASTER_USER)) {
			Submenu clientSubmenu = new Submenu();
			clientSubmenu.setLabel("Client");
			clientSubmenu.setIcon("ui-icon-suitcase");

			MenuItem addClientMenuItem = new MenuItem();
			addClientMenuItem.setValue("Add");
			addClientMenuItem.setUrl("/protected/clientMaster/clientManagement.xhtml");
			addClientMenuItem.setIcon("ui-icon-document");
			clientSubmenu.getChildren().add(addClientMenuItem);

			MenuItem listClientMenuItem = new MenuItem();
			listClientMenuItem.setValue("List");
			listClientMenuItem.setUrl("/protected/master/clientList.xhtml");
			listClientMenuItem.setIcon("ui-icon-print");
			clientSubmenu.getChildren().add(listClientMenuItem);

			menuModel.addSubmenu(clientSubmenu);
		} else if (loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
			MenuItem clientMenuItem = new MenuItem();
			clientMenuItem.setValue("Client");
			clientMenuItem.setUrl("/protected/clientMaster/clientManagement.xhtml");
			clientMenuItem.setIcon("ui-icon-suitcase");
			menuModel.addMenuItem(clientMenuItem);
		}

		// User
		if (loggedUserRole.equals(Role.MASTER_USER) || loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
			Submenu userSubmenu = new Submenu();
			userSubmenu.setLabel("User");
			userSubmenu.setIcon("ui-icon-person");

			MenuItem addUserMenuItem = new MenuItem();
			addUserMenuItem.setValue("Add");
			addUserMenuItem.setUrl("/protected/client/userManagement.xhtml");
			addUserMenuItem.setIcon("ui-icon-document");
			userSubmenu.getChildren().add(addUserMenuItem);

			MenuItem listUserMenuItem = new MenuItem();
			listUserMenuItem.setValue("List");
			listUserMenuItem.setUrl("/protected/clientMaster/userList.xhtml");
			listUserMenuItem.setIcon("ui-icon-print");
			userSubmenu.getChildren().add(listUserMenuItem);

			menuModel.addSubmenu(userSubmenu);
		} else if (loggedUserRole.equals(Role.CLIENT_USER)) {
			MenuItem userMenuItem = new MenuItem();
			userMenuItem.setValue("User");
			userMenuItem.setUrl("/protected/client/userManagement.xhtml");
			userMenuItem.setIcon("ui-icon-person");
			menuModel.addMenuItem(userMenuItem);
		}

		// Log Source
		if (loggedUserRole.equals(Role.MASTER_USER) || loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
			Submenu logSourceSubmenu = new Submenu();
			logSourceSubmenu.setLabel("Log Source");
			logSourceSubmenu.setIcon("ui-icon-signal-diag");

			MenuItem addLogSourceMenuItem = new MenuItem();
			addLogSourceMenuItem.setValue("Add");
			addLogSourceMenuItem.setUrl("/protected/clientMaster/logSourceManagement.xhtml");
			addLogSourceMenuItem.setIcon("ui-icon-document");
			logSourceSubmenu.getChildren().add(addLogSourceMenuItem);

			MenuItem listLogSourceMenuItem = new MenuItem();
			listLogSourceMenuItem.setValue("List");
			listLogSourceMenuItem.setUrl("/protected/clientMaster/logSourceList.xhtml");
			listLogSourceMenuItem.setIcon("ui-icon-print");
			logSourceSubmenu.getChildren().add(listLogSourceMenuItem);

			menuModel.addSubmenu(logSourceSubmenu);
		}

		// Tag
		if (loggedUserRole.equals(Role.MASTER_USER) || loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
			Submenu tagSubmenu = new Submenu();
			tagSubmenu.setLabel("Tag");
			tagSubmenu.setIcon("ui-icon-tag");

			MenuItem addTagMenuItem = new MenuItem();
			addTagMenuItem.setValue("Add");
			addTagMenuItem.setUrl("/protected/clientMaster/tagManagement.xhtml");
			addTagMenuItem.setIcon("ui-icon-document");
			tagSubmenu.getChildren().add(addTagMenuItem);

			MenuItem listTagMenuItem = new MenuItem();
			listTagMenuItem.setValue("List");
			listTagMenuItem.setUrl("/protected/clientMaster/tagList.xhtml");
			listTagMenuItem.setIcon("ui-icon-print");
			tagSubmenu.getChildren().add(listTagMenuItem);

			menuModel.addSubmenu(tagSubmenu);
		}

		// View
		if (loggedUserRole.equals(Role.MASTER_USER)) {
			Submenu viewSubmenu = new Submenu();
			viewSubmenu.setLabel("View");
			viewSubmenu.setIcon("ui-icon-image");

			Submenu dynamicViewSubmenu = new Submenu();
			dynamicViewSubmenu.setLabel("Dynamic");
			dynamicViewSubmenu.setIcon("ui-icon-play");
			viewSubmenu.getChildren().add(dynamicViewSubmenu);

			MenuItem listDynamicViewMenuItem = new MenuItem();
			listDynamicViewMenuItem.setValue("List");
			listDynamicViewMenuItem.setUrl("/protected/master/dynamicViewList.xhtml");
			listDynamicViewMenuItem.setIcon("ui-icon-print");
			dynamicViewSubmenu.getChildren().add(listDynamicViewMenuItem);

			Submenu staticViewSubmenu = new Submenu();
			staticViewSubmenu.setLabel("Static");
			staticViewSubmenu.setIcon("ui-icon-pause");
			viewSubmenu.getChildren().add(staticViewSubmenu);

			MenuItem addStaticViewMenuItem = new MenuItem();
			addStaticViewMenuItem.setValue("Add");
			addStaticViewMenuItem.setUrl("/protected/clientMaster/staticViewManagement.xhtml");
			addStaticViewMenuItem.setIcon("ui-icon-document");
			staticViewSubmenu.getChildren().add(addStaticViewMenuItem);

			MenuItem listStaticViewMenuItem = new MenuItem();
			listStaticViewMenuItem.setValue("List");
			listStaticViewMenuItem.setUrl("/protected/clientMaster/staticViewList.xhtml");
			listStaticViewMenuItem.setIcon("ui-icon-print");
			staticViewSubmenu.getChildren().add(listStaticViewMenuItem);

			menuModel.addSubmenu(viewSubmenu);
		} else if (loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
			Submenu viewSubmenu = new Submenu();
			viewSubmenu.setLabel("View");
			viewSubmenu.setIcon("ui-icon-image");

			MenuItem dynamicViewMenuItem = new MenuItem();
			dynamicViewMenuItem.setValue("Dynamic");
			dynamicViewMenuItem.setUrl("/protected/clientMaster/dynamicViewManagement.xhtml");
			dynamicViewMenuItem.setIcon("ui-icon-play");
			viewSubmenu.getChildren().add(dynamicViewMenuItem);

			Submenu staticViewSubmenu = new Submenu();
			staticViewSubmenu.setLabel("Static");
			staticViewSubmenu.setIcon("ui-icon-pause");
			viewSubmenu.getChildren().add(staticViewSubmenu);

			MenuItem addStaticViewMenuItem = new MenuItem();
			addStaticViewMenuItem.setValue("Add");
			addStaticViewMenuItem.setUrl("/protected/clientMaster/staticViewManagement.xhtml");
			addStaticViewMenuItem.setIcon("ui-icon-document");
			staticViewSubmenu.getChildren().add(addStaticViewMenuItem);

			MenuItem listStaticViewMenuItem = new MenuItem();
			listStaticViewMenuItem.setValue("List");
			listStaticViewMenuItem.setUrl("/protected/clientMaster/staticViewList.xhtml");
			listStaticViewMenuItem.setIcon("ui-icon-print");
			staticViewSubmenu.getChildren().add(listStaticViewMenuItem);

			menuModel.addSubmenu(viewSubmenu);
		}

		// Search
		if (loggedUserRole.equals(Role.CLIENT_MASTER_USER) || loggedUserRole.equals(Role.CLIENT_USER)) {
			MenuItem searchMenuItem = new MenuItem();
			searchMenuItem.setValue("Search");
			searchMenuItem.setUrl("/protected/client/search.xhtml");
			searchMenuItem.setIcon("ui-icon-search");
			menuModel.addMenuItem(searchMenuItem);
		}

		// Logout
		MenuItem logoutMenuItem = new MenuItem();
		logoutMenuItem.setValue("Logout");
		logoutMenuItem.setActionExpression(createActionExpression("#{menuBean.logout}"));
		logoutMenuItem.setIcon("ui-icon-close");
		menuModel.addMenuItem(logoutMenuItem);
	}

	public MenuModel getMenuModel() {
		return menuModel;
	}

	public String logout() {
		logoutUser();
		return "/public/login.xhtml?faces-redirect=true";
	}
}
